package com.kaili.controller.pda.v1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.client.service.ILoginUserInfoService;
import com.kaili.common.exception.BaseExcept;
import com.kaili.common.tenant.DbContextHolder;
import com.kaili.common.util.*;
import com.kaili.db.service.IHsBasicSeqService;
import com.kaili.db.service.IHsEwbsStowageService;
import com.kaili.db.service.IHsOptEwbAppointService;
import com.kaili.db.service.IHsOptEwbService;
import com.kaili.except.ResponseStatus;
import com.kaili.feign.UserFeignClient;
import com.kaili.pojo.dto.ewb.CustomerDTO;
import com.kaili.pojo.dto.ewb.EwbAddDTO;
import com.kaili.pojo.dto.ewb.EwbQueryAppointsDTO;
import com.kaili.pojo.dto.ewb.EwbQueryDTO;
import com.kaili.pojo.entity.HsOptEwb;
import com.kaili.pojo.vo.AppointQueryVO;
import com.kaili.pojo.vo.CustomerVO;
import com.kaili.pojo.vo.EwbQueryVO;
import com.kaili.pojo.vo.EwbSummaryVO;
import com.kaili.utils.DateUtils;
import com.kaili.version.ApiVersion;
import freemarker.template.Configuration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController("ewbControllerP")
@Api(tags = "[pda]托运开单服务", description = "[pda]托运开单相关API")
public class EwbController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MessageSource messageSource;

    @Autowired
    IHsOptEwbService ewbService;

    @Autowired
    IHsEwbsStowageService stowageService;

    @PostMapping(value = "/p/ewbs/bat")
    @ApiOperation(value = "[pda]新增批量开单", notes = "[pda]新增批量开单")
    public List<Map<String,Object>> doBathAddEwb(@Validated @RequestBody ValidList<EwbAddDTO> addBeanList) {
        List<Map<String,Object>> list=new ArrayList<>();
        for (EwbAddDTO ewbAddDTO :addBeanList) {
            Integer sendCustomerId = ewbAddDTO.getSendCustomerId();
            Integer receiveCustomerId = ewbAddDTO.getReceiveCustomerId();
            if(sendCustomerId==null){
                CustomerVO sendCustVO=new CustomerVO();
                BeanUtils.copyProperties(ewbAddDTO.getSendCustomer(),sendCustVO);
                sendCustVO.setCustomerCode(ewbService.getMaxCustomerCode());
                sendCustVO.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
                sendCustVO.setCreateTime(new Date());
                sendCustVO.setSettlement(ewbAddDTO.getSettleModeId());
                sendCustVO.setSite(ewbAddDTO.getSendSiteId());
                ewbService.addCustomer(sendCustVO);
                ewbAddDTO.setSendCustomerId(sendCustVO.getCustomerId());
            }

            if(receiveCustomerId==null){
                CustomerVO receiveCustVO=new CustomerVO();
                BeanUtils.copyProperties(ewbAddDTO.getReceiveCustomer(),receiveCustVO);
                receiveCustVO.setCustomerCode(ewbService.getMaxCustomerCode());
                receiveCustVO.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
                receiveCustVO.setCreateTime(new Date());
                receiveCustVO.setSettlement(ewbAddDTO.getSettleModeId());
                receiveCustVO.setSite(ewbAddDTO.getSendSiteId());
                ewbService.addCustomer(receiveCustVO);
                ewbAddDTO.setReceiveCustomerId(receiveCustVO.getCustomerId());
            }
            ewbAddDTO.setEwbDate(new Date());
            ResponseStatus status = updateEwb(ewbAddDTO);
            HashMap<String, Object> map = new HashMap<>();
            map.put("status",status.getCode());
            map.put("message",messageSource.getMessage(status.getMsg(), null, CommonUtil.getLocale()));
            map.put("ewbNo", ewbAddDTO.getEwbNo());
            list.add(map);
        }
        return list;
    }

    private ResponseStatus updateEwb(EwbAddDTO addBean) {
        // 1.校验
        ResponseStatus result = com.kaili.controller.common.v1.EwbController.check(ewbService,stowageService,addBean);
        if(200 != result.getCode()){
            return result;
        }

        // 2.保存或修改
        int ret = ewbService.doAddEwb(addBean);
        if (ret != 0) {
            return ResponseStatus.EWB_ADD_ERR;
        }
        return ResponseStatus.RESPONSE_OK;
    }
}